home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / paul_qureshi / info / quake.txt < prev    next >
Text File  |  1999-03-27  |  10KB  |  353 lines

  1.  
  2. This document contains the transparencies for my talk at the Computer Game Developers Conference on 
  3. 4/2/96.  Audio and video tapes of the talk are available from the conference organizers.  Note that the 
  4. Quake technology described in these transparencies can be licensed from id Software; contact Jay Wilbur 
  5. (jayw@idsoftware.com), or Mike Wilson (mikew@idsoftware.com).
  6. --Michael Abrash
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. The Quake Graphics Engine
  14.  
  15.  
  16. Michael Abrash
  17.  
  18.  
  19. id Software
  20.  
  21.  
  22.  
  23. WARNING!
  24. Explicit technical content!!
  25.  
  26. A highly technical look inside the design of the Quake graphics engine.
  27.  
  28. 1 hour != 12 months
  29.  
  30. Idea is to give you benefit of our real-world experience to help you up the learning curve.
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. "Pretty soon, computers will be fast."
  38.                                 -Billy Zelsnack
  39.  
  40.  
  41.  
  42. Hardware isn't as fast as we'd like for realtime 3-D, and isn't going to be for a long time.
  43.  
  44. Hard frame-rate constraints.
  45.  
  46.  
  47.  
  48.  
  49. Fun thing #1: hardware is fast enough to do interesting things, but not fast enough to do what we really 
  50. want.
  51.  
  52. Fun thing #2: a ton of techniques and approaches, all of which are good for something, none of which are 
  53. good for everything.
  54.  
  55. Balancing act between visual quality and speed, all with the frame rate limit as a hard constraint--and it 
  56. all changes every year as hardware moves forward.
  57.  
  58.  
  59. Objectives:
  60.  
  61. Quake:
  62. True, arbitrary 6 DOF 3-D.
  63. Highest possible graphics quality,
  64.    both lighting and rasterizing.
  65. True 3-D for players, monsters, and
  66.    as many objects as possible.
  67.  
  68. Any 3-D engine:
  69. Correct color at each pixel.
  70. Adequate, reasonably level framerate.
  71.  
  72. For our purposes, that means sampling the right pixel from the polygon that's nearest at that pixel, and 
  73. doing so at 10-15 frame/second minimum.
  74.  
  75.  
  76. It would be nice to be able to chuck all the polygons at a rasterizer that was so fast that we didn't have to 
  77. think any further.
  78.  
  79. Problem: no such rasterizer.
  80.  
  81. Bigger problem: level designers would just use more polygons.
  82.  
  83. The number of polygons goes up very fast with larger/more complex worlds, much more than linearly, 
  84. though the number of visible polygons may not.
  85.  
  86. Problem of rejecting polygons outside the view pyramid, and frame rate variation.
  87.  
  88. Principle #1: we need to reduce the polygon set quickly to only those that matter, or at least those that 
  89. have a pretty good chance of mattering.
  90.  
  91. Principle #2: we need to get the right colors on the right pixels for those polygons we do draw; this means 
  92. perspective correction, z-ordering, shading, subpixel and subtexel placement, and so on.
  93.  
  94. #2 has been the focus for a long time, but #1 is quickly becoming the real challenge.  Both must be done 
  95. well in order to build a compelling engine
  96.  
  97.  
  98.  
  99. Two parts to the Quake graphics engine:
  100.  
  101. Static world.
  102.  
  103. Everything else (moving objects, including players, monsters, doors, platforms, health, ammo, armor, and 
  104. so on).
  105.  
  106. These are handled very differently.
  107.  
  108.  
  109.  
  110. The static world.
  111.  
  112. Big (up to around 10,000 large polygons), with a continuous polygon mesh skin and any number of light 
  113. sources.
  114.  
  115. Can be preprocessed very effectively.
  116.  
  117. Is compiled into a single BSP tree.
  118.  
  119.  
  120.  
  121. Handy facts about BSP trees:
  122.  
  123. Nodes are splitting planes, with polygons stored on them, that carve up space.
  124.  
  125. Leaves are the convex volumes that nodes carve the world into.
  126.  
  127. A BSP tree can be walked front-to-back or back-to-front in linear time for any viewpoint.
  128.  
  129. A BSP tree performs a convex, hierarchical partitioning of space, with each node defining a subspace that 
  130. contains all of the node's children.
  131.  
  132. Hierarchical partitioning helps greatly in large-scale culling.  Node bounding boxes can be clipped to the 
  133. view pyramid.
  134.  
  135. If a node's bounding box is trivially rejected, all its children can be rejected with no further testing.
  136.  
  137. If a node is trivially accepted, all its children can be accepted with no further testing.
  138.  
  139.  
  140. The other part of large-scale culling:  culling polygons that are in the view pyramid but are totally 
  141. obscured.  
  142. Some of the techniques that were considered and/or tried:
  143.  
  144. Z-buffering
  145. Painters algorithm
  146. Beam tree
  147. Subdividing raycasting
  148. Edge or span sorting
  149. Portals
  150. Direct visibility extraction from BSP
  151.  
  152. In every case, some average performance impact, considerable worst-case impact, so frame rate wasn't 
  153. very level.
  154.  
  155.  
  156.  
  157.  
  158. Precalculate the PVS!
  159.  
  160. Precalculated the PVS from each leaf to each leaf.
  161.  
  162. Virtually free at runtime.
  163.  
  164. Very expensive to precalculate--but that's what preprocessing is for.
  165.  
  166. Speeds up culling to the view pyramid and determination of moving objects that have to be drawn, 
  167. because only nodes and leaves in the PVS matter.
  168.  
  169.  
  170.  
  171. Frame rate was still not level enough, because of overdraw.
  172.  
  173. Added another stage, edge sorting, to process the polygons in the PVS.
  174.  
  175. Add edges to global table, process all edges at once, top to bottom, emit nearest spans.  Later, draw the 
  176. spans for each surface in turn.
  177.  
  178.  
  179.  
  180. Extra sorting work, but reduced overdraw to zero, levelling performance a great deal.
  181.  
  182. Other benefits:
  183.  
  184. Shared edges
  185. Concave polygons
  186. Culling of polygons for 3-D hw
  187.  
  188.  
  189.  
  190. What key to sort on?
  191.  
  192. BSP order.
  193.  
  194. 1/z.
  195.  
  196. BSP order.
  197.  
  198. BSPs contain more implicit information that you might think.
  199.  
  200.  
  201.  
  202. Rasterization!
  203.  
  204. Disadvantages of normal Gouraud shaded texture mapping:
  205.  
  206. Rotational lighting variance if
  207.    triangles aren't used.
  208.  
  209. Lighting detail is added by adding
  210.    more polygons.
  211.  
  212. Lighting is not perspective correct.
  213.  
  214. Solution: surface caching
  215.  
  216.  
  217.  
  218. Each surface is generated from a combination of a tiled texture and a light map with light values on a 16-
  219. pixel grid.
  220.  
  221. This is cached and used as the source texture for a non-shading texture mapper.
  222.  
  223. The light map is precalculated with ray casting from lights to each grid point on each surface.  Radiosity 
  224. would be possible.
  225.  
  226. Per-surface mipmapping helps keep the total cache size down, and also improves appearance at a distance.
  227.  
  228.  
  229.  
  230. Advantages:
  231.  
  232. No rotational variance.
  233. Perspective correct lighting.
  234. Fewer polgyons.
  235. Lighting separated from polygons.
  236. Ability to post-process.
  237.  
  238. Disadvantages:
  239.  
  240. More memory.
  241. More cache misses.
  242. Slow for constantly changing lighting
  243.    (fine for switching lighting on/off).
  244. Slowdown when turning corners.
  245. Not a perfect fit for hardware.
  246.  
  247.  
  248.  
  249. Time to draw!
  250.  
  251. There's a span drawer that takes the list of spans for a surface and draws the pixels.
  252.  
  253. No lighting is performed.
  254.  
  255. The screen gradients for the texture coordinates and 1/z are used to calculate perspective correct texture 
  256. coordinates every 8 or 16 pixels, and linear interpolation is used between those points.
  257.  
  258.  
  259.  
  260. 100% floating-point down to the 8 or 16 pixel subdivisions.
  261.  
  262. Hurts on 486, but Pentium floating-point is fast, and floating-point solves all sorts of range and scale 
  263. problems, and allows both lowering FDIV precision and overlapping FDIV.  Also, using FP registers frees 
  264. up integer registers.
  265.  
  266. All in all, our inner loop is clearly faster in floating-point on Pentium than it would be in fixed-point
  267.  
  268.  
  269.  
  270. Everything else--moving entities.
  271.  
  272. There are several types of entities, stored as BSP trees, polygon meshes, sprites, and particles.
  273.  
  274. Large-scale culling benefits for entities:  Each entity's bounding box is clipped into the world BSP.
  275.  
  276. Only if it touches a PVS leaf is it flagged for later drawing, after the world has been processed.
  277.  
  278.  
  279.  
  280. Separate BSP models, such as doors, platforms, and health and ammo boxes, are clipped into the world 
  281. BSP, then added to the global edge table, just like world polygons.
  282.  
  283. Doors block view past them, reducing overdraw considerably.
  284.  
  285. BSP models in the same leaf fall back to sorting against each other on 1/z.
  286.  
  287.  
  288.  
  289. More complex objects, such as torches, armor, and players can't be done with BSPs.
  290.  
  291. Sprites look startlingly non-3D in a Quake level; they're rare now, and may disappear entirely, but are an 
  292. easy solution for objects like torches.
  293.  
  294. We had been clipping sprites into the world BSP and drawing the pieces by their leaves' BSP order, 
  295. drawing back to front.
  296.  
  297. That approach didn't work so well for polygon models--players and monsters
  298.  
  299.  
  300.  
  301. Polygon models are meshes of 100-400 polygons, with a single front/back skin stretched over them.
  302.  
  303. We couldn't clip them to the world BSP, because it would be too expensive, so we just drew each triangle 
  304. in the nearest leaf it had a vertex in, which caused occasional errors.
  305.  
  306. Errors sorting polygons within models.
  307.  
  308. Errors sorting between models in same leaf (and also with other BSP models and sprites).
  309.  
  310.  
  311.  
  312. Tried lots of tweaks (such as putting into edge list), but none was satisfactory until John came up with a 
  313. sweeping solution:
  314.  
  315. Uh... Z-buffering
  316.  
  317. We z-buffer sprites, polygon models, and all other non-BSP entities.
  318.  
  319. How low-tech can you get?
  320.  
  321.  
  322.  
  323. Solved all sorting problems for moving entities with NO sorting errors ever.
  324.  
  325. Fits beautifully into overall design.
  326.  
  327. Because of no-overdraw spans, can z-fill world at an adequate speed.
  328.  
  329. Allows post-processing.  At the moment, we're doing particles, scaled NxN colored rectangles, but could 
  330. do pretty much anything.
  331.  
  332. Low-tech, high-tech, whatever; the secret's in the mixing and matching until it all fits
  333.  
  334.  
  335. In conclusion:
  336.  
  337. The more 3-D tricks you know, and the more you experiment, the better.
  338.  
  339. Yes, I could use a vacation.
  340.  
  341. Sharing knowledge makes the world a better place.
  342.  
  343. Further reading:
  344.  
  345. "Ramblings in Realtime," Dr. Dobb's Sourcebook, 1995-1996.
  346.  
  347. Zen of Graphics Programming, 2nd ed., the Coriolis Group, April 1996.
  348.  
  349. Computer Graphics, Foley, van Dam, et al, Addison-Wesley.
  350.  
  351. Procedural Elements for Computer Graphics, David Rogers, McGraw Hill.
  352.  
  353.